import { createRouter, createWebHistory } from 'vue-router'
import MyRegister from '../components/Register.vue'
import MyHome from '../components/MyHome.vue'
import Dashboard from '../components/DashboardUI.vue'
import Profile from '../views/DoctorProfile.vue' // 医生个人中心组件
import EducationActivities from '../views/EducationActivities.vue' // 患教活动组件
import Consultations from '../components/ConsultationsUI.vue' // 看诊记录组件
import AddConsultation from "../components/AddConsultation.vue"; // 添加看诊记录组件
import Patients from "../views/PatientsUI.vue"; // 患者信息列表组件
import PatientProfile from '../views/PatientProfile.vue' // <-- 患者个人信息组件
import UserLogin from '@/components/UserLogin.vue'
import PatientSymptomsReactions from '../views/PatientSymptomsReactions.vue'

const routes = [
    { path: '/', redirect: '/login' },  // 路由重定向，默认跳登录页
    { path: '/login', component: UserLogin  },
    { path: '/register', name: 'Register', component: MyRegister },
    {
        // 统一处理 /home 路由及其所有子路由
        path: '/home',
        component: MyHome,
        children: [
            {
                path: 'dashboard', // 访问路径为 /home/dashboard
                component: Dashboard,
                name: 'Dashboard',
            },
            // --- 医生专用路由 ---
            {
                path: 'profile', // 医生个人中心，访问路径为 /home/profile
                component: Profile,
                name: 'DoctorProfile', // 建议改名为 DoctorProfile 以避免与 PatientProfile 混淆
            },
            {
                path: 'education', // 患教活动，访问路径为 /home/education
                component: EducationActivities,
                name: 'EducationActivities',
            },
            {
                path: 'consultations', // 看诊记录，访问路径为 /home/consultations
                component: Consultations,
                name: 'Consultations',
            },
            {
                path: 'add_Consultation', // 添加看诊，访问路径为 /home/add_Consultation
                component: AddConsultation,
                name: 'AddConsultation',
            },
            {
                path: 'patients', // 患者信息，访问路径为 /home/patients
                component: Patients,
                name: 'Patients',
            },
            {
                path: 'symptoms', // <-- 患者症状及不良反应记录路由
                component: PatientSymptomsReactions,
                name: 'PatientSymptomsReactions',
            },
            // --- 患者专用路由 ---
            {
                path: 'patient_my_records', // 患者看诊记录，访问路径为 /home/patient_my_records
                // TODO: 替换为实际的患者看诊记录组件
                // component: PatientMyRecords, // 假设你需要一个 PatientMyRecords.vue
                name: 'PatientMyRecords',
                component: () => import('../views/PatientMyRecords.vue'), // 示例：假设文件名为 views/PatientMyRecords.vue
            },
            {
                path: 'patient_my_profile', // 患者个人信息，访问路径为 /home/patient_my_profile
                component: PatientProfile, // <-- 这里是我们新添加的患者个人信息组件
                name: 'PatientProfile',
            },
            {
                path: 'user_dashboard',
                component: () => import('../views/DashboardPatient.vue'),
                name: 'DashboardPatient'
            },
            {
                path: 'my_consultations',
                component: () => import('../views/MyConsultations.vue'),
                name: 'MyConsultations'
            }
        ],
    },
]

const router = createRouter({
    history: createWebHistory(),
    routes,
})

// 路由守卫，登录权限控制 (保持不变)
router.beforeEach((to, from, next) => {
    const token = localStorage.getItem('token') || localStorage.getItem('isLogin')

    if (to.path === '/login' || to.path === '/register') {
        if (token) {
            next('/home/dashboard')
        } else {
            next()
        }
    } else if (to.path.startsWith('/home')) {
        if (token) {
            next()
        } else {
            next('/login')
        }
    } else {
        next()
    }
})

export default router